#include "lvgl/lvgl.h"
#include "lvgl/demos/lv_demos.h"
#include "lv_drivers/display/fbdev.h"
#include "lv_drivers/indev/evdev.h"
#include <unistd.h>
#include <pthread.h>
#include <time.h>
#include <sys/time.h>
#include "lv_chinese_ime/lv_chinese_ime.h"
#include "lv_chinese_ime/src/chinese_ime.h"

#define DISP_BUF_SIZE (128 * 1024)

lv_obj_t *btn ;
lv_obj_t *label;

static void event_handler(lv_event_t * e)
{
    static uint8_t status = 0 ;
    lv_event_code_t code = lv_event_get_code(e);
    if(code == LV_EVENT_CLICKED)
    {
        switch(status)
        {
        case 0:
            lv_label_set_text(label,"こんにちは");
            status = 1 ;
            break ;
        case 1:
            lv_label_set_text(label,"Hallo China");
            status = 2 ;
            break ;
        case 2:
            lv_label_set_text(label,"Привет Китай");
            status = 3 ;
            break ;
        case 3:
            lv_label_set_text(label,"你好中国");
            status = 0 ;
            break ;
        default:
            break ;
        }
    }
}

 static void ta_event_cb(lv_event_t * e)
{
    lv_event_code_t code = lv_event_get_code(e);
    lv_obj_t * ta = lv_event_get_target(e);
    lv_obj_t * kb = lv_event_get_user_data(e);

    if(code == LV_EVENT_FOCUSED) {
        if(lv_indev_get_type(lv_indev_get_act()) != LV_INDEV_TYPE_KEYPAD) {
            lv_keyboard_set_textarea(kb, ta);
            lv_obj_set_style_max_height(kb, LV_HOR_RES * 2 / 3, 0);
            lv_obj_clear_flag(kb, LV_OBJ_FLAG_HIDDEN);
            lv_obj_scroll_to_view_recursive(ta, LV_ANIM_OFF);
        }
    }
    else if(code == LV_EVENT_READY || code == LV_EVENT_CANCEL) {
        lv_obj_add_flag(kb, LV_OBJ_FLAG_HIDDEN);
        lv_obj_clear_state(ta, LV_STATE_FOCUSED);
        lv_indev_reset(NULL, ta);   /*To forget the last clicked object to make it focusable again*/
    }
}

int main(void)
{
    /*LittlevGL init*/
    lv_init();

    /*Linux frame buffer device init*/
    fbdev_init();

    /*A small buffer for LittlevGL to draw the screen's content*/
    static lv_color_t buf[DISP_BUF_SIZE];

    /*Initialize a descriptor for the buffer*/
    static lv_disp_draw_buf_t disp_buf;
    lv_disp_draw_buf_init(&disp_buf, buf, NULL, DISP_BUF_SIZE);

    /*Initialize and register a display driver*/
    static lv_disp_drv_t disp_drv;
    lv_disp_drv_init(&disp_drv);
    disp_drv.draw_buf   = &disp_buf;
    disp_drv.flush_cb   = fbdev_flush;
    disp_drv.hor_res    = 480;
    disp_drv.ver_res    = 800;
    lv_disp_drv_register(&disp_drv);

    evdev_init();
    static lv_indev_drv_t indev_drv_1;
    lv_indev_drv_init(&indev_drv_1); /*Basic initialization*/
    indev_drv_1.type = LV_INDEV_TYPE_POINTER;

    /*This function will be called periodically (by the library) to get the mouse position and state*/
    indev_drv_1.read_cb = evdev_read;
    lv_indev_t *mouse_indev = lv_indev_drv_register(&indev_drv_1);


    /*Set a cursor for the mouse*/
    LV_IMG_DECLARE(mouse_cursor_icon)
    lv_obj_t * cursor_obj = lv_img_create(lv_scr_act()); /*Create an image object for the cursor */
    lv_img_set_src(cursor_obj, &mouse_cursor_icon);           /*Set the image source*/
    lv_indev_set_cursor(mouse_indev, cursor_obj);             /*Connect the image  object to the driver*/


    /*Create a Demo*/
    //lv_demo_widgets();
    btn = lv_btn_create(lv_scr_act());
    lv_obj_set_size(btn, 100, 50);
    lv_obj_center(btn);
    lv_obj_set_style_bg_color(btn,  lv_color_hex(0xE3EAF4), LV_STATE_DEFAULT);
    lv_obj_set_style_bg_color(btn,  lv_color_hex(0x3E6094), LV_STATE_PRESSED);
    lv_obj_add_event_cb(btn, event_handler, LV_EVENT_ALL, NULL);


    LV_FONT_DECLARE(siyuan_black);
    static lv_style_t style_font ;
    lv_style_init(&style_font);
    label = lv_label_create(lv_scr_act());
    lv_obj_set_size(label, 200,100);
    lv_obj_set_style_text_font(label, &siyuan_black, 0);
    lv_label_set_text(label,"你好中国");

    //创建一个键盘输入控件
    LV_FONT_DECLARE(lv_font_source_han_sans_bold_14);
    lv_obj_t *keyboard = lv_chinese_ime_create(lv_scr_act());
    lv_chinese_ime_set_text_font(&lv_font_source_han_sans_bold_14,0);
    //创建一个文本输入框
    lv_obj_t *ta1 = lv_textarea_create(lv_scr_act());
    lv_obj_set_size(ta1, 100,50);
    lv_obj_set_style_text_font(ta1, &lv_font_source_han_sans_bold_14, 0);
    lv_obj_align(ta1, LV_ALIGN_TOP_LEFT, 0, 50);
    lv_keyboard_set_textarea(keyboard, ta1);
	lv_textarea_set_max_length(ta1, 20);
    lv_obj_add_event_cb(ta1, ta_event_cb, LV_EVENT_ALL, keyboard);
    //默认不让输入键盘弹出来
    lv_obj_add_flag(keyboard, LV_OBJ_FLAG_HIDDEN);
    //lv_obj_clear_state(ta1, LV_STATE_FOCUSED);
    lv_indev_reset(NULL, ta1);   /*To forget the last clicked object to make it focusable again*/

    /*Handle LitlevGL tasks (tickless mode)*/
    while(1) {
        lv_timer_handler();
        usleep(5000);
    }

    return 0;
}

/*Set in lv_conf.h as `LV_TICK_CUSTOM_SYS_TIME_EXPR`*/
uint32_t custom_tick_get(void)
{
    static uint64_t start_ms = 0;
    if(start_ms == 0) {
        struct timeval tv_start;
        gettimeofday(&tv_start, NULL);
        start_ms = (tv_start.tv_sec * 1000000 + tv_start.tv_usec) / 1000;
    }

    struct timeval tv_now;
    gettimeofday(&tv_now, NULL);
    uint64_t now_ms;
    now_ms = (tv_now.tv_sec * 1000000 + tv_now.tv_usec) / 1000;

    uint32_t time_ms = now_ms - start_ms;
    return time_ms;
}
